package com.bindapp.model;

import android.content.Context;
import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.support.v7.widget.LinearLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.bindapp.R;
import com.bindapp.activity.DeviceDetailActivity;
import com.bindapp.activity.DevicePlayActivity;
import com.bindapp.activity.JKGLActivity;
import com.bindapp.activity.SBGLActivity;
import com.bindapp.activity.WebActivity;
import com.bindapp.adapter.MainTab2Adapter;
import com.bindapp.databinding.FragmentMaintab2Binding;
import com.bindapp.listener.LoadStatusListener;
import com.bindapp.listener.RecyclerItemClick;
import com.bindapp.util.PreferencesUtil;
import com.bindapp.util.Static;
import com.bindapp.util.ezutil.EZUtils;
import com.bindapp.widget.PopupWindow.CommonPopupWindow;
import com.bindapp.widget.RVItemDecoration;
import com.videogo.constant.IntentConsts;
import com.videogo.openapi.bean.EZCameraInfo;
import com.videogo.openapi.bean.EZDeviceInfo;
import com.youth.banner.listener.OnBannerListener;

import java.util.ArrayList;
import java.util.List;

import io.reactivex.Observable;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.annotations.NonNull;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Function;
import io.reactivex.schedulers.Schedulers;

import static com.bindapp.App.getOpenSDK;

/**
 * Created by tom055 on 2018/6/3.
 */

public class Tab2Model extends BaseModel {
    private FragmentMaintab2Binding binding;
    private MainTab2Adapter adapter;
    private List<Object> datalist=new ArrayList<Object>();

    private List<Object> datadevicelist=new ArrayList<Object>();

    private String[] titles=new String[]{"公共区海豚池","公共区贝壳池","VIP区海螺池"};

    private CommonPopupWindow morepopupWindow;
    LoadStatusListener listener;

    public Tab2Model(final Context context, LoadStatusListener listener){
        super(context);
        this.listener=listener;
        binding = DataBindingUtil.inflate(LayoutInflater.from(context), R.layout.fragment_maintab2,null,false);
        textData();
        initView();

    }

    private void textData(){
        for(int i=0;i<3;i++){
            datalist.add(""+i);
        }
    }

    private void initView(){
        adapter=new MainTab2Adapter(context,datalist,R.layout.item_tab2_device,0,new ItemClick(),new HeadBannerClick());
        adapter.addHeadView(R.layout.item_tab2_head);
        binding.setClick(new ViewClick());
        binding.recycleview.setAdapter(adapter);
        binding.recycleview.addItemDecoration(new RVItemDecoration(0));
        binding.recycleview.setLayoutManager(new LinearLayoutManager(context, LinearLayoutManager.VERTICAL, false));
    }

    public class ViewClick{
        public void onClick(View view){
            switch (view.getId()){
                case R.id.morebtn:
                    morepopupWindow = new CommonPopupWindow.Builder(context)
                                    .setView(R.layout.pop_device_more)
                                    .setWidthAndHeight(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT)
                                    .setAnimationStyle(R.style.AnimDown)
                                    .setViewOnclickListener(new PopViewClick())
                                    .setOutsideTouchable(true)
                                    .create();
                    morepopupWindow.showAsDropDown(binding.morebtn);
                    break;
                case R.id.label1:
                    morepopupWindow = new CommonPopupWindow.Builder(context)
                            .setView(R.layout.pop_device_dp)
                            .setWidthAndHeight(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT)
                            .setAnimationStyle(R.style.AnimDown)
                            .setViewOnclickListener(new PopViewClick())
                            .setOutsideTouchable(true)
                            .create();
                    morepopupWindow.showAsDropDown(binding.label1);
                    break;
            }
        }
    }

    public void getData(){
        listener.hide(true,"正在加载中...");
            Observable.fromArray(1)
                    .subscribeOn(Schedulers.io())
                    .map(new Function<Integer, Object>() {

                        @Override
                        public Object apply(@NonNull Integer integer) throws Exception {
                            datadevicelist.clear();
                            datadevicelist.addAll(getOpenSDK().getDeviceList(0, 20));
                            return "";
                        }
                    })
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(new Observer<Object>() {
                        @Override
                        public void onNext(Object o) {
                            listener.show();
                        }

                        @Override
                        public void onComplete() {

                        }

                        @Override
                        public void onError(Throwable e) {
                            listener.hide(false,"数据异常");
                        }

                        @Override
                        public void onSubscribe(Disposable d) {
                        }
                    });


    }


    class PopViewClick implements CommonPopupWindow.ViewInterface{

        @Override
        public void getChildView(View view, int layoutResId) {
            switch (layoutResId){

                case R.layout.pop_device_more:

                    TextView managedevice=view.findViewById(R.id.manage_device);
                    managedevice.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            morepopupWindow.dismiss();
                            Intent intent=new Intent(context,SBGLActivity.class);
                            context.startActivity(intent);
                        }
                    });

                    TextView managejk=view.findViewById(R.id.manage_jk);
                    managejk.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            morepopupWindow.dismiss();
                            Intent intent=new Intent(context,JKGLActivity.class);
                            context.startActivity(intent);
                        }
                    });

                    break;
                case R.layout.pop_device_dp:
                    view.findViewById(R.id.label1).setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            morepopupWindow.dismiss();
                        }
                    });
                    break;

            }
        }
    }

    class HeadBannerClick implements OnBannerListener{

        @Override
        public void OnBannerClick(int position) {

        }
    }

    class ItemClick implements RecyclerItemClick{

        @Override
        public void onItemClick(int position) {
            Intent intent = new Intent(context,DeviceDetailActivity.class);
            intent.putExtra("title",titles[position-1]);
            context.startActivity(intent);

        }

        @Override
        public void onItemLongClick(int position) {

        }

        @Override
        public void onMoreClick(View view,int position) {
            Intent intent=null;
            EZDeviceInfo deviceInfo=(EZDeviceInfo)datadevicelist.get(0);
            EZCameraInfo cameraInfo = EZUtils.getCameraInfoFromDevice(deviceInfo, 0);
            switch (view.getId()){
                case R.id.jkview:

                    intent=new Intent(context,DevicePlayActivity.class);
                    intent.putExtra("title",titles[position-1]);
                    intent.putExtra(IntentConsts.EXTRA_DEVICE_INFO, deviceInfo);
                    intent.putExtra(IntentConsts.EXTRA_CAMERA_INFO, cameraInfo);
                    break;
                case R.id.carmeraBtn:
                    //intent=new Intent(context, JKGLActivity.class);
                    intent=new Intent(context,DevicePlayActivity.class);
                    intent.putExtra("title",titles[position-1]);
                    intent.putExtra(IntentConsts.EXTRA_DEVICE_INFO, deviceInfo);
                    intent.putExtra(IntentConsts.EXTRA_CAMERA_INFO, cameraInfo);
                    break;
                case R.id.cjview:
                    intent=new Intent(context,WebActivity.class);
                    intent.putExtra("title","幸运大抽奖");
                    intent.putExtra("url", Static.CJUrl+ PreferencesUtil.getInstance(context).getCookie());

                    break;
                /*case R.id.devicelabel:
                    intent=new Intent(context, SBGLActivity.class);
                    break;*/
            }

            context.startActivity(intent);

        }
    }

    @Override
    public FragmentMaintab2Binding getBinding(){
        return binding;
    }
}
